<?php

namespace app\index\service;

use app\common\model\Knowledge;
use app\index\dao\KnowledgeDao;
use app\index\model\UM as UM;
use think\Collection;

/**
 * Class KnowledgeRelatedService
 * @package app\index\service
 */
class KnowledgeRelatedService
{
    // dao
    private KnowledgeDao $knowledgeDao;

    public function __construct(KnowledgeDao $knowledgeDao)
    {
        $this->knowledgeDao = $knowledgeDao;
    }

    /**
     * @param UM|null $user
     * @param $id
     * @param int|null $page
     * @param int|null $page_size
     * @return Collection
     */
    public function index(?UM $user, $id, ?int $page = null, ?int $page_size = null): Collection
    {
        $record = Knowledge::where('id', $id)
            ->append(['smallPicImg'])
            ->hidden(['smallPic'])
            ->find();

        if (!$record || !is_id($record->categoryId)) {
            return \think\model\Collection::make();
        }

        $categoryId = $record->categoryId;

        $excluded = [$record->id];

        return $this->knowledgeDao->select($categoryId, null, null, $excluded, $page, $page_size);
    }

}
